package us.icitap.commandcenter.ui.shared;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.utils.Utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.sun.xml.messaging.saaj.util.ByteInputStream;
import com.vaadin.server.BrowserWindowOpener;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.JavaScriptFunction;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.CloseEvent;

public class CallInfoWindow extends Window{


	
	public CallInfoWindow( final Call call, final CallTable table) {
		this.center();
		this.setResizable(false);
		setModal(true);
		this.setCaption("Informacion mbi telefonaten");
		Label l = new Label(call.getToolTip(), ContentMode.HTML);
		VerticalLayout vl = new VerticalLayout(l);
		
		
		RelatedCallsVerticalLayout rcVl = new RelatedCallsVerticalLayout(call);
		vl.addComponent(rcVl);
		
		this.setContent(vl);
		
		this.addCloseListener(new CloseListener() {
			
			@Override
			public void windowClose(CloseEvent e) {
				call.setInfoDisplayed(true);
				table.updateCall(call);
				
			}
		});
		
		
	}
	
	public CallInfoWindow( final Call call) {
		this.center();
		this.setResizable(false);
		setModal(true);
		this.setCaption("Informacion mbi telefonaten");
		Label l = new Label(call.getToolTip(), ContentMode.HTML);
		VerticalLayout vl = new VerticalLayout(l);
		
		vl.setId("CALLINFO");
		RelatedCallsVerticalLayout rcVl = new RelatedCallsVerticalLayout(call);
		vl.addComponent(rcVl);
		
		this.setContent(vl);
		
//		Button print = new Button("Print");
//		vl.addComponent(print);
//		
//		
//		final Label ll = new Label();
//		vl.addComponent(ll);
//		
//		Page.getCurrent().getJavaScript().addFunction("dumpcontent",
//			    new JavaScriptFunction() {
//			    @Override
//			    public void call(JSONArray arguments)
//			            throws JSONException {
//			        ll.setValue(arguments.getString(0));
//			        String html = "<html><body>" + ll.getValue() + "</body></html>";
//			        html = html.replace("\"", "'");
//					ll.setValue(html);
//			    }    
//			});
//		
		
//		
//		Page.getCurrent().getJavaScript().execute(
//	            "dumpcontent(document.getElementById('CALLINFO').innerHTML)");
//		
//		
//		
//		
//		print.addClickListener(new ClickListener() {
//			
//			@Override
//			public void buttonClick(ClickEvent event) {
//				 Document document = new Document();
//
//				    PdfWriter writer = null;
//					try {
//						writer = PdfWriter.getInstance(document,
//
//						    new FileOutputStream("c:\\temp\\test.pdf"));
//					} catch (FileNotFoundException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} catch (DocumentException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//
//				    document.open();
//
//				    try {
//						XMLWorkerHelper.getInstance().parseXHtml(writer, document,
//
//						    new ByteInputStream(ll.getValue().getBytes(), ll.getValue().getBytes().length));
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//
//				    document.close();
//				
//			}
//		});
		
		
		    
		    
		
	}
	
	
	public void show(){
		CommandCenterUI.getCurrent().addWindow(this);
	}
	
	
}
